<?php

namespace App\Controller\Wap;

use App\Entity\ShopGoods;
use App\Entity\UserLikesProduct;
use App\Model\MoneyLogModel;
use App\Model\MoneyModel;
use App\Model\ShopGoodsModel;
use App\Model\UserLikesProductModel;
use App\Base\BaseConfig;
use App\Base\BaseController4User;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\PlatConfigModel;
use App\Model\LazyCommon\UserInviteExModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;


/**
 * @route("/wap")
 */
class TeamController extends BaseController4User
{

    /**
     * @route("/team/reg",name="UserAppTeamReg")
     */
    public function UserAppTeamReg()
    {
        $uid = $this->needToBeUser();
        $UserInviteExModel = new UserInviteExModel($this->getDoctrine());


        $data = $UserInviteExModel->getSonsTree($uid,3);
        $ids = $UserInviteExModel->getSonsIds($data,$ids,"reg");
//        $userIdArray = array();

        $level1Num = 0;
        $level2Num = 0;
        $level3Num = 0;
        if (isset($ids[1])) {
            $level1Num = count($ids[1]);
        }
        if (isset($ids[2])) {
            $level2Num = count($ids[2]);
        }
        if (isset($ids[3])) {
            $level3Num = count($ids[3]);
        }

        $this->data["level1num"] = $level1Num;
        $this->data["level2num"] = $level2Num;
        $this->data["level3num"] = $level3Num;


        $userIdArray = array();

        if (isset($ids[1])) {
            $userIdArray = array_merge($userIdArray,$ids[1]);
        }
        if (isset($ids[2])) {
            $userIdArray = array_merge($userIdArray,$ids[2]);
        }
        if (isset($ids[3])) {
            $userIdArray = array_merge($userIdArray,$ids[3]);
        }

        $users = $this->getUserFromList4Controller($userIdArray,0);

        $this->data["users"] = $users;
        $this->data["level"] = 0;
        $this->data["groups"] = 0;
        return $this->render("App/Wap/team_reg.php.twig",$this->data);
    }

    /**
     * @route("/team/buy",name="UserAppTeamBuy")
     */
    public function UserAppTeamBuy()
    {
        $uid = $this->needToBeUser();
        $UserInviteExModel = new UserInviteExModel($this->getDoctrine());


        $data = $UserInviteExModel->getSonsTree($uid,3);
        $ids = $UserInviteExModel->getSonsIds($data,$ids,"buy");

        $level1Num = 0;
        $level2Num = 0;
        $level3Num = 0;
        if (isset($ids[1])) {
            $level1Num = count($ids[1]);
        }
        if (isset($ids[2])) {
            $level2Num = count($ids[2]);
        }
        if (isset($ids[3])) {
            $level3Num = count($ids[3]);
        }

        $this->data["level1num"] = $level1Num;
        $this->data["level2num"] = $level2Num;
        $this->data["level3num"] = $level3Num;
        $this->data["level"] = 0;
        $this->data["groups"] = 0;
        return $this->render("App/Wap/team_buy.php.twig",$this->data);
    }
    /**
     * @route("/team/buy/json",name="UserAppTeamJson")
     */
    public function UserAppTeamJson()
    {
        $uid = $this->needToBeUser();
        if(!$level = $this->getParamRequired("level")){$level = 0;}
        if(!$groups = $this->getParamRequired("groups")){$groups = 0;}
        if(!$type = $this->getParamRequired("type")){$type = "";}//reg挑选未购买 buy挑选购买过的 “”挑选全部

        $UserInviteExModel = new UserInviteExModel($this->getDoctrine());
        $data = $UserInviteExModel->getSonsTree($uid,3);


        $ids = $UserInviteExModel->getSonsIds($data,$ids,$type);

        $userIdArray = array();
        if ( $level == 0 ) {
            if (isset($ids[1])) {
                $userIdArray = array_merge($userIdArray,$ids[1]);
            }
            if (isset($ids[2])) {
                $userIdArray = array_merge($userIdArray,$ids[2]);
            }
            if (isset($ids[3])) {
                $userIdArray = array_merge($userIdArray,$ids[3]);
            }
        }else{
            if (isset($ids[$level])) {
                $userIdArray = $ids[$level];
            }
        }
//        $this->data["idArray"] =$userIdArray;
        $this->data["ids"] = $ids;
        if (!$userIdArray) {

            $this->data["users"] = [];
            return $this->json_out(1,"没有此类数据",$this->data);
        }

        $this->data["users"] = $this->getUserFromList4Controller($userIdArray,$groups);
        return $this->json_out(1,"数据获取成功",$this->data);
    }


    private function getUserFromList4Controller($userIdArray,$groups)
    {
        if (!$userIdArray) {
            return [];
        }
        $UserInviteExModel = new UserInviteExModel($this->getDoctrine());
        $qb = $UserInviteExModel->em->createQueryBuilder();

        $qb->from("App:User","A")
            ->select("A.id,A.account,A.groups,A.lastLoginTime,A.phone,A.headPic,A.nickName,B.cur")
            ->leftJoin("App:Money","B","with","A.id = B.uid")
            ->where(
                $qb->expr()->in("A.id",$userIdArray)
            );
        if ($groups) {
            $qb->andWhere("A.groups = ".(int)$groups);
        }
        $userList = $qb->getQuery()->getResult();
//        $this->toArray()
        foreach ($userList as &$row) {
            if (!$row["phone"]) {
                $row["phone"] = "尚未绑定手机号";
            }
            if (!$row["nickName"]) {
                $row["nickName"] = $row["account"];
            }
            if (!$row["headPic"]) {
                $row["headPic"] = BaseConfig::__USER_PIC_DEFAULT__;
            }
            if (!$row["lastLoginTime"]) {
                $row["lastLoginTime"] = "-";
            }else{
                $row["lastLoginTime"] = date(YYMMDDHHIISS,$row["lastLoginTime"]);
            }
        }
        return $userList;

    }


}
